package com.xiaoxiang.text.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaoxiang.common.result.HttpResult;
import com.xiaoxiang.text.entity.Dynamic;
import com.xiaoxiang.text.service.DynamicService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;

/**
 * (Dynamic)表控制层
 *
 * @author haha
 * @since 2022-03-22 13:16:47
 */
@RestController
@RequestMapping("dynamic")
public class DynamicController {
    /**
     * 服务对象
     */
    @Resource
    private DynamicService dynamicService;

    /**
     * 推荐动态
     *
     */
    @GetMapping("recommend/list")
    public HttpResult getRecommendList(@RequestParam(value = "size", defaultValue = "5")
                                                Integer size) {
        return HttpResult.success(this.dynamicService.getRecommendList(size));
    }

    /**
     * 点赞
     *
     */
    @GetMapping("like")
    public HttpResult like(@RequestParam Long userId, @RequestParam Long dynamicId) {
        return HttpResult.success(this.dynamicService.like(dynamicId, userId));
    }


    /**
     * 取消点赞
     *
     */
    @GetMapping("cancelLike")
    public HttpResult cancelLike(@RequestParam Long userId, @RequestParam Long dynamicId) {
        return HttpResult.success(this.dynamicService.cancelLike(dynamicId, userId));
    }

    /**
     * 浏览
     *
     */
    @GetMapping("browse")
    public HttpResult browse(@RequestParam Long userId, @RequestParam Long dynamicId) {
        return HttpResult.success(this.dynamicService.browse(dynamicId, userId));
    }



    /**
     * 分页查询所有数据
     *
     */
    @GetMapping("list")
    public HttpResult selectAll(Page<Dynamic> page, Dynamic dynamic) {
        return HttpResult.success(this.dynamicService.pageList(page, new QueryWrapper<>(dynamic)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public HttpResult selectOne(@PathVariable Serializable id) {
        return HttpResult.success(this.dynamicService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param dynamic 实体对象
     * @return 新增结果
     */
    @PostMapping
    public HttpResult insert(@RequestBody Dynamic dynamic) {
        dynamic.setHeat(0L);
        dynamic.setPraise(0L);
        dynamic.setComment(0L);
        return HttpResult.success(this.dynamicService.save(dynamic));
    }

    /**
     * 修改数据
     *
     * @param dynamic 实体对象
     * @return 修改结果
     */
    @PutMapping
    public HttpResult update(@RequestBody Dynamic dynamic) {
        return HttpResult.success(this.dynamicService.updateById(dynamic));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping
    public HttpResult delete(@RequestParam("id") Long id) {
        return HttpResult.success(this.dynamicService.removeById(id));
    }
}
